package Viewer;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import DataModel.Feature;
import DataModel.FeatureHolder;
import DataModel.LociHolder;

/**
 * The feature table contains all features of a track.<br>
 * Enables a quick searching and sorting.
 * 
 * @author Stefan Lorenz<br>
 * <br>
 *         28.11.2012
 */
@SuppressWarnings("serial")
public class FeatureTable extends JTable implements MouseListener {

    /**
     * The browsing session window in GeneScapes
     */
    private Viewer viewer;
    /**
     * Table model
     */
    private DefaultTableModel model;

    /**
     * Constructor of the feature table
     * 
     * @param viewer
     */
    public FeatureTable(Viewer viewer) {
	this.viewer = viewer;

	// model.fireTableDataChanged();

	this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	this.addMouseListener(this);
	String[] columnNames = { "Number:", "Name:", "Type:", "Start:", "End:", "Direction:", "Locus:" };

	this.model = new DefaultTableModel(new String[0][7], columnNames) {
	    @Override
	    public Class<?> getColumnClass(int column) {
		switch (column) {
		case 0:
		    return Integer.class;
		case 1:
		    return String.class;
		case 2:
		    return String.class;
		case 3:
		    return Integer.class;
		case 4:
		    return Integer.class;
		case 5:
		    return String.class;
		case 6:
		    return String.class;
		default:
		    return Object.class;
		}
	    }
	};
	isCellEditable(1, 1);
	this.setModel(model);
	this.setAutoCreateRowSorter(true);
    }

    /**
     * Fills the table with all features of a specific track
     * 
     * @param trackIndex
     *            index of the track
     */
    public void fillTable(int trackIndex) {
	if (viewer.getLoadedTracks()[trackIndex] == null)
	    return;
	int count = 0;

	String[][] tempdata = null;
	if (viewer.getLoadedTracks()[trackIndex].getTypeOfFile().contains("gff") || viewer.getLoadedTracks()[trackIndex].getTypeOfFile().contains("bed") || viewer.getLoadedTracks()[trackIndex].getTypeOfFile().contains("gtf") || viewer.getLoadedTracks()[trackIndex].getTypeOfFile().contains("sam")) {
	    FeatureHolder rff = (FeatureHolder) viewer.getLoadedTracks()[trackIndex];
	    tempdata = new String[rff.size()][7];

	    for (int l = 0; l < rff.size(); l++) {
		tempdata[l][0] = String.valueOf(l);
		tempdata[l][1] = rff.get(l).getName();
		tempdata[l][2] = rff.get(l).getType();
		tempdata[l][3] = String.valueOf(rff.get(l).getAbsoultePositions().getStart());
		tempdata[l][4] = String.valueOf(rff.get(l).getAbsoultePositions().getStop());
		tempdata[l][5] = rff.get(l).getStrand();
		tempdata[l][6] = rff.get(l).getLocus();
	    }

	} else if (viewer.getLoadedTracks()[trackIndex].getTypeOfFile().contains("gbk") || viewer.getLoadedTracks()[trackIndex].getTypeOfFile().contains("embl")) {
	    LociHolder genome = (LociHolder) viewer.getLoadedTracks()[trackIndex];
	    for (int i = 0; i < genome.getListOfLoci().get(0).getBucketList().size(); i++) {
		count += genome.getListOfLoci().get(0).getBucketList().get(i).getFeaturesOfBucket().size();
	    }
	    tempdata = new String[count][7];
	    count = 0;
	    for (int i = 0; i < genome.getListOfLoci().get(0).getBucketList().size(); i++) {
		for (int j = 0; j < genome.getListOfLoci().get(0).getBucketList().get(i).getFeaturesOfBucket().size(); j++) {
		    Feature f = genome.getListOfLoci().get(0).getBucketList().get(i).getFeaturesOfBucket().get(j);

		    tempdata[count][0] = String.valueOf(count);
		    tempdata[count][1] = f.getName();
		    tempdata[count][2] = f.getType();
		    tempdata[count][3] = String.valueOf(f.getAbsoultePositions().getStart());
		    tempdata[count][4] = String.valueOf(f.getAbsoultePositions().getStop());
		    tempdata[count][5] = f.getStrand();
		    tempdata[count][6] = f.getLocus();
		    count++;
		}
	    }

	}

	String[] columnNames = { "Number:", "Name:", "Type:", "Start:", "End:", "Direction:", "Locus:" };
	this.model = new DefaultTableModel(tempdata, columnNames);
	isCellEditable(1, 1);

	this.setModel(model);
    }

    public boolean isCellEditable(int x, int y) {
	return false;
    }

    public void mouseClicked(MouseEvent e) {
	int diff = Integer.valueOf(getValueAt(this.getSelectedRow(), 3).toString()) - this.viewer.getPositionOfBrowser(1);
	if (e.getClickCount() == 2 && e.getButton() == 1) {
	    for (int i = 0; i < this.viewer.getPositionOfTrack().length; i++) {
		this.viewer.setPositionOfBrowser(i, this.viewer.getPositionOfBrowser(i) + diff + -(this.viewer.getRangeOfBrowser() / 2));
	    }
	}
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
}
